/**
Custom module for you to write your own javascript functions
**/
jQuery(document).ready(function () {

    App.init(); // initlayout and core plugins

    //init datetime picker
    $('.date-picker').datepicker({
        rtl: App.isRTL(),
        autoclose: true
    });
    //init datatable paging sort
    initDatatable();
    initSelectedRow();

});
var initSelectedRow = function() {
    $(".tbl-selected tbody tr").click(function() {
        $(this).addClass("row-selected");
        $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
    });
};
var initDatatable = function() {
    if ($('.dataTable').length > 0) {
        $('.dataTable').each(function() {
            LoadDataTable($(this));
        });
    }
};
function LoadDataTable(dttb) {
    if ($('.dataTable').length > 0) {
        $('.dataTable').each(function () {
            var opt = {
                "sPaginationType": "bootstrap",
                "oLanguage": {
                    "sSearch": "<span>Search:</span> ",
                    "sInfo": "Showing <span>_START_</span> to <span>_END_</span> of <span>_TOTAL_</span> entries",
                    "sLengthMenu": '<select class="form-control">' +
                        '<option value="10">10</option>' +
                        '<option value="25">25</option>' +
                        '<option value="50">50</option>' +
                        '<option value="100">100</option>' +
                        '<option value="-1">All</option>' +
                        "</select> records",
                }
            };
            if ($(this).hasClass("dataTable-noheader")) {
                opt.bFilter = false;
                opt.bLengthChange = false;
            }
            if ($(this).hasClass("dataTable-nofooter")) {
                opt.bInfo = false;
                opt.bPaginate = false;
            }
            if ($(this).hasClass("dataTable-customsort")) {
                opt.aaSorting = [];
                opt.aoColumnDefs = [];
                var dataTables = $(this);
                for (var i = 0; i < (dataTables.length) ; i++) {
                    var columns = dataTables[i].getElementsByTagName("th");
                    for (var x = 0; x < (columns.length) ; x++) {
                        if (jQuery(columns[x]).hasClass("sort-desc")) {
                            opt.aaSorting.push([x, 'desc']);
                        } else
                            if (jQuery(columns[x]).hasClass("sort-asc")) {
                                opt.aaSorting.push([x, 'asc']);
                            }
                            else if (jQuery(columns[x]).hasClass("nosort")) {
                                opt.aoColumnDefs.push({ "bSortable": false, 'aTargets': [x] });
                            }
                    }
                }
            }


            if ($(this).hasClass("dataTable-nosort")) {
                var column = $(this).data('nosort');
                column = column.split(',');
                for (var i = 0; i < column.length; i++) {
                    column[i] = parseInt(column[i]);
                }
                ;
                opt.aoColumnDefs = [
                    { 'bSortable': false, 'aTargets': column }
                ];
            }
            if ($(this).hasClass("dataTable-scroll-x")) {
                opt.sScrollX = "100%";
                opt.bScrollCollapse = true;
            }
            if ($(this).hasClass("dataTable-scroll-y")) {
                opt.sScrollY = "100px";
                opt.bPaginate = false;
                opt.bScrollCollapse = true;
            }
            if ($(this).hasClass("dataTable-reorder")) {
                opt.sDom = "Rlfrtip";
            }
            if ($(this).hasClass("dataTable-colvis")) {
                opt.sDom = 'C<"clear">lfrtip';
                opt.oColVis = {
                    "buttonText": "Change columns <i class='icon-angle-down'></i>"
                };
            }

            if ($(this).hasClass('dataTable-tools')) {
                opt.sDom = 'T<"clear">lfrtip';
                opt.oTableTools = {
                    "sSwfPath": "js/plugins/datatable/swf/copy_csv_xls_pdf.swf"
                };
            }
            if ($(this).hasClass("dataTable-scroller")) {
                opt.sScrollY = "300px";
                opt.bDeferRender = true;
                opt.sDom = "frtiS";
                opt.sAjaxSource = "js/plugins/datatable/demo.txt";
            }
            var qs = (function (a) {
                if (a == "") return {};
                var b = {};
                for (var i = 0; i < a.length; ++i) {
                    var p = a[i].split('=');
                    if (p.length != 2) continue;
                    b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
                }
                return b;
            })(window.location.search.substr(1).split('&'));

            if ($(this).hasClass("dataTable-noscroll")) {
                opt.sDom = "Rlfrtip";
            }
            var oTable = $(this).dataTable(opt);
            try {
                if (parseInt(qs["p"]) > 0) {
                    oTable.fnPageChange(parseInt(qs["p"]) - 1);
                }
            }
            catch (exception) { }

            oTable.on('draw', function () {
                if ($('#dttb_activepage').val() != undefined) {
                    $('#dttb_activepage').val(oTable.fnPagingInfo().iPage + 1);
                }
                if ($(this).hasClass("tbl-selected")) {
                    initSelectedRow();
                }
            });
            $('.dataTables_filter input').attr("class", "form-control input-medium");
            if ($(this).hasClass("dataTable-columnfilter")) {
                oTable.columnFilter({
                    "sPlaceHolder": "head:after"
                });
            }
            if ($(this).hasClass("dataTable-reorder")) {
                opt.sDom = "Rlfrtip";
            }
        });
    }
}
function PopupCenter(pageURL, w, h) {
    var left = (screen.width / 2) - (w / 2);
    var top = (screen.height / 2) - (h / 2) - 50;
    return window.open(pageURL, 'popup', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=1, resizable=1, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
}

function Popup(pageURL, w, h) {
    return window.open(pageURL, 'popup', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=1, resizable=1, copyhistory=no, width=' + w + ', height=' + h);
}



